
package TransportesMarroquin;

import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField;

import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel;



public class AyudaPilotoVehiculo extends javax.swing.JFrame {

ResultSetTableModel tableModel;
boolean Cabezal = false;
String consulta = "select idpiloto, nombre, apellido, licencia from Piloto";
    public AyudaPilotoVehiculo(boolean c) {
        initComponents();
        Cabezal = c;
    }

    private AyudaPilotoVehiculo() {
       
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbus = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        cd = new javax.swing.JLabel();
        lnum2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cbus.setText("Nombre del Piloto");
        cbus.setName("cbus"); // NOI18N
        cbus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbusMouseClicked(evt);
            }
        });
        cbus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbusActionPerformed(evt);
            }
        });
        cbus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cbusKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cbusKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cbusKeyTyped(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1316234747_search_b.png"))); // NOI18N
        jButton1.setText("Buscar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        try{
            tableModel = new ResultSetTableModel( consulta );
        }catch(Exception e){
            JOptionPane.showMessageDialog(null,"Error:"+e);
        }
        resultTable.setModel(tableModel
        );
        resultTable.setName("resultTable");
        resultTable.getTableHeader().setReorderingAllowed(false);
        resultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultTableMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                resultTableMouseEntered(evt);
            }
        });
        jScrollPane1.setViewportView(resultTable);

        jPanel1.setBackground(new java.awt.Color(143, 0, 0));
        jPanel1.setName("jPanel1"); // NOI18N

        cd.setFont(new java.awt.Font("Tahoma", 1, 18));
        cd.setForeground(new java.awt.Color(255, 255, 255));
        cd.setText(" ");
        cd.setName("cd"); // NOI18N

        lnum2.setFont(new java.awt.Font("Tahoma", 1, 18));
        lnum2.setForeground(new java.awt.Color(255, 255, 255));
        lnum2.setText("Búsqueda de Piloto -por Nombre-");
        lnum2.setName("lnum2"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lnum2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cd)
                .addGap(87, 87, 87))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cd)
                    .addComponent(lnum2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cbus, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbusActionPerformed
            
    }//GEN-LAST:event_cbusActionPerformed

    private void cbusKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbusKeyPressed
     
    }//GEN-LAST:event_cbusKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            NuevoViajePiloto.cnom.setEnabled(true);
            NuevoViajePiloto.cape.setEnabled(true);
            NuevoViajePiloto.cd.setEnabled(true);
            NuevoViajePiloto.clic.setEnabled(true);
                       
            NuevoViajePiloto.cnom.setText("");
            NuevoViajePiloto.cape.setText("");
            NuevoViajePiloto.cd.setText("");
            NuevoViajePiloto.clic.setText("");
            
            // configuro nuevo para qee además de ingresar datos lo ingrese a mi base de datos como cliente. 
            NuevoViajePiloto.nuevo=true;
            
            dispose ();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void cbusKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbusKeyTyped
        
    }//GEN-LAST:event_cbusKeyTyped

    private void cbusKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbusKeyReleased
       String con = cbus.getText();
      consulta = "select idpiloto, nombre, apellido, licencia from piloto where nombre like '"+con+"%'";
      try{

tableModel = new ResultSetTableModel( consulta );
}
catch ( Exception e) {
 JOptionPane.showMessageDialog( null,
 e );}
      resultTable.setModel(tableModel);
    }//GEN-LAST:event_cbusKeyReleased

    private void resultTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseClicked
         resultTable.setFocusable(true);
         int y= resultTable.rowAtPoint(evt.getPoint());
     if(Cabezal)
     NuevoCabezal.cd.setText(""+resultTable.getValueAt(y, 0));
    else
         if(!Cabezal)
             NuevoCamion.cd.setText(""+resultTable.getValueAt(y, 0));
    
   
     dispose();
    }//GEN-LAST:event_resultTableMouseClicked

    private void resultTableMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultTableMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_resultTableMouseEntered

    private void cbusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbusMouseClicked
cbus.setText("");        // TODO add your handling code here:
    }//GEN-LAST:event_cbusMouseClicked

   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AyudaPilotoVehiculo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cbus;
    private javax.swing.JLabel cd;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lnum2;
    public final javax.swing.JTable resultTable = new javax.swing.JTable();
    // End of variables declaration//GEN-END:variables
}
